(function () {
  layui.use(["table", "element", "form", "layer", "laydate"], function () {
    var table = layui.table;
    var element = layui.element;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var $ = layui.jquery;
    var url = GlobalSiteUrl + "/moment/getStrategy";
    var sid = "";
    // console.log(url)

    const data1 = "<option value='0'>未推荐</option>";
    const data2 = "<option value='1'>推荐</option>";
    $("#introStateSelector").append(data1, data2);
    const data3 = "<option value='0'>未审核</option>";
    const data4 = "<option value='1'>已审核</option>";
    $("#passStateSelector").append(data3, data4);
    form.render("select");

    //第一个实例
    table.render({
      elem: "#table",
      toolbar: "#toolbarDemo",
      width: "auto",
      height: "full - 100",
      url: url, //数据接口
      response: {
        statusCode: 200, //重新规定成功的状态码为 200，table 组件默认为 0
      },
      parseData: function (res) {
        // console.log(res)
        if (res.data == null) {
          alert(res.msg);
          location.reload();
          return;
        } else {
          var data = res.data.list;
          data.forEach((element) => {
            element["createTime"] = formatTime(element["createTime"]);
            element["updateTime"] = !element["updateTime"]
              ? "暂未更新"
              : formatTime(element["updateTime"]);
          });
          if (!res.data.total === 0) {
            return {
              code: res.status,
              msg: res.msg,
              data: res.data,
              count: 1,
            };
          } else {
            return {
              code: res.status,
              msg: res.msg,
              data: data,
              count: res.data.total,
            };
          }
        }
      },
      page: true, //开启分页
      method: "get",
      id: "reloadTable",
      cols: [
        [
          {
            field: "sid",
            title: "sid",
            align: "sid",
            width: 190,
          },
          {
            field: "title",
            title: "标题",
            align: "title",
            width: 150,
          },
          {
            field: "description",
            title: "描述",
            align: "center",
            width: 180,
          },
          {
            field: "taps",
            title: "标签",
            align: "center",
            width: 180,
          },
          {
            field: "name",
            title: "发布者",
            align: "center",
            width: 110,
          },
          {
            field: "images",
            title: "图片",
            align: "center",
            width: 170,
            templet: "#img",
            event: "img",
          },
          {
            field: "createTime",
            title: "创建时间",
            align: "center",
            width: 180,
            sort: true,
          },
          {
            field: "updateTime",
            title: "更新时间",
            align: "center",
            width: 180,
            sort: true,
          },
          {
            fixed: "right",
            align: "center",
            field: "type",
            title: "推荐",
            width: 130,
            templet: "#display",
            unresize: true,
          },
          {
            fixed: "right",
            align: "center",
            title: "操作",
            toolbar: "#barDemo",
            width: 180,
          },
        ],
      ],
    });

    //监听行工具事件
    table.on("tool(table)", function (obj) {
      var data = obj.data;
      console.log(data);
      if (obj.event === "img") {
        var img = "";
        var images = data.images;
        for (var i in images) {
          img += "<img src='" + images[i].url + "' class='img'>";
        }
        var image =
          "<div class='" +
          "layui-upload-list" +
          "'id='" +
          "demo2" +
          "'>" +
          img +
          "</div>";
        layer.open({
          area: ["1020px", "500px"],
          type: 1,
          title: "查看图片",
          value: data.images[0],
          content: image,
          btn: "关闭",
          yes: function (index) {
            layer.closeAll();
          },
        });
      }

      // 删除
      if (obj.event === "delete") {
        var url =
          GlobalSiteUrl +
          "/moment/deleteStrategy?sid=" +
          obj.data.sid
        layer.confirm(
          "确认删除？",
          {
            btn: ["确定", "取消"],
            title: "提示",
          },
          function (index) {
            $.post({
              url: url,
              success: function (res) {
                if (res.status == 200) {
                  layer.msg("删除成功");
                  layer.close(index);
                  setTimeout(function () {
                    location.reload();
                  }, 1000);
                } else {
                  layer.msg("删除失败", {
                    time: 1500,
                    anim: 6,
                  });
                  layer.close(index);
                }
              },
            });
            layer.close(index);
          }
        );
        // 审核通过
      } else if (obj.event === "pass") {
        var url =
          GlobalSiteUrl +
          "/moment/updateStrategy?sid=" +
          obj.data.sid +
          "&delState=1";
        layer.confirm(
          "确认通过？",
          {
            btn: ["确定", "取消"],
            title: "提示",
          },
          function (index) {
            $.post({
              url: url,
              success: function (res) {
                // console.log(res);
                if (res.status == 200) {
                  layer.msg("状态更新成功");
                  layer.close(index);
                  setTimeout(function () {
                    location.reload();
                  }, 1000);
                } else {
                  layer.msg("状态更新失败", {
                    time: 1500,
                    anim: 6,
                  });
                  layer.close(index);
                }
              },
            });
            layer.close(index);
          }
        );
      }
    });

    //按钮事件
    var $ = layui.$,
      active = {
        reload: function () {
          // 页面重载&模糊查询
          var search = $("#search");
          // console.log(search.val());
          //执行重载
          table.reload(
            "reloadTable",
            {
              // header:'application/x-www-form-urlencoded',
              page: {
                curr: 1, //重新从第 1 页开始
              },
              where: {
                mobileName: search.val(),
              },
            },
            "data"
          );
        },
      };

    // 按键监听
    $(".demoTable .layui-btn").on("click", function () {
      var type = $(this).data("type");
      active[type] ? active[type].call(this) : "";
    });

    form.on("checkbox(display)", function (obj) {
      console.log(obj);
      var type = "";
      if (obj.elem.checked) {
        type = 1;
      } else {
        type = 0;
      }
      $.ajax({
        type: "POST",
        url:
          GlobalSiteUrl +
          "/moment/updateStrategy?type=" +
          type +
          "&sid=" +
          obj.elem.dataset.sid,
        success: function (res) {
          if (res.status === 200) {
            if (type === 1) {
              layer.msg("设为推荐成功");
            } else {
              layer.msg("取消推荐");
            }
          } else {
            layer.msg("推荐失败");
          }
        },
      });
    });

    // 下拉框按状态查询
    form.on("select(stateSelect)", function (data) {
      // console.log(data)
      var state = data.value;
      table.reload(
        "reloadTable",
        {
          page: {
            curr: 1, //重新从第 1 页开始
          },
          where: {
            state: state,
          },
        },
        "data"
      );
    });

    // 下拉框按手机型号查询
    form.on("select(passStateSelector)", function (data) {
      var delState = data.value;
      table.reload(
        "reloadTable",
        {
          page: {
            curr: 1, //重新从第 1 页开始
          },
          where: {
            delState,
          },
        },
        "data"
      );
    });
    // 下拉框选择手机品牌
    form.on("select(introStateSelector)", function (data) {
      var type = data.value;
      table.reload(
        "reloadTable",
        {
          page: {
            curr: 1, //重新从第 1 页开始
          },
          where: {
            type,
          },
        },
        "data"
      );
    });
  });
})();
